// 创建 store
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({

  // state 专门放数据
  state: {
    count: 1,
    showTab: true,
    swiperList: [],
    msiteList: [],
    searchList: [],
    list: [],
    foodRecommendation: [],
    filterList: [],
    secondaryList: [],
    reqId: -100,
    hideDetail: false,
    shopDetailsList: {},
    weatherList: '',
    orderState: true,
    isShowDis: false,
    isLoaded: false,
    district: '大学城创客小镇',
    disList: [],
    location: {
      latitude: 22.5832338,
      longitude: 113.9569707
    },
    listReqObj: {
      latitude: 22.5832338,
      longitude: 113.9569707,
      keyword: '',
      offset: 0,
      limit: 20,
      'extras[]': 'activities'
    },
    shopDetailsMenu: [],
    carts: [],
    ulikeList: [],
    ulikeViewArr: [],
    searchWord: []
  },
  // mutations 唯一能修改state的地方
  mutations: {
    ADDCART: function (state, id) {
      // 先判断是否有这个商品，
      var isZD = false
      for (var i = 0; i < state.carts.length; i++) {
        if (state.carts[i].id === id) {
          isZD = true
          // 找到之后给数据 ++
          state.carts[i].count++
          break
        }
      }

      if (!isZD) {
        var obj = {
          id: id,
          count: 1
        }
        state.carts.push(obj)
      }
    },
    ADD: function (state) {
      state.count++
    },
    SHOWTAB: function (state) {
      state.showTab = true
    },
    HIDETAB: function (state) {
      state.showTab = false
    },
    HIDEDETAIL: function (state) {
      state.hideDetail = !state.hideDetail
    },
    GETSWIPERDATA: function (state) {
      axios.get('http://localhost:3000/entries', {
        params: {
          latitude: state.location.latitude,
          longitude: state.location.longitude,
          'templates[]': 'main_template'
        }
      }).then(function (res) {
        console.log(res)
        state.swiperList = res.data[0].entries
      }).catch(function (err) {
        console.log(err)
      })
    },
    SHOWDISSELECT: function (state) {
      state.isShowDis = true
    },
    HIDEDISSELECT: function (state) {
      state.isShowDis = false
    },
    GETMSITELIST: function (state) {
      axios.get('http://localhost:3000/msitelist', {
        params: state.listReqObj
      }).then(function (res) {
        console.log(res)
        state.msiteList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    MOREMSITELIST: function (state) {
      axios.get('http://localhost:3000/msitelist', {
        params: Object.assign({}, state.listReqObj, {
          limit: state.listReqObj.limit + 20
        })
      }).then(function (res) {
        console.log(res)
        state.listReqObj = Object.assign({}, state.listReqObj, {limit: state.listReqObj.limit + 20})
        state.msiteList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    RECOMMENDATION: function (state) {
      axios.get('http://localhost:3000/discover', {
        params: state.location
      }).then(function (res) {
        // console.log(res)
        var foodData = res.data
        state.foodRecommendation = foodData
        // console.log(state.foodRecommendation)
      }).catch(function (err) {
        console.log(err)
      })
    },
    SHOPDETAILSLIST: function (state, id) {
      axios.get('http://localhost:3000/shop', {
        params: Object.assign({}, state.location, {
          restaurant_id: id
        })
      }).then(function (res) {
        // console.log(res)
        var shopData = res.data
        console.log(shopData)
        state.shopDetailsList = shopData
      }).catch(function (err) {
        console.log(err)
      })
    },
    GETFILTERLIST: function (state) {
      axios.get('http://localhost:3000/filterlist', {
        params: state.location
      }).then(function (res) {
        console.log(res)
        state.filterList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    SWITCHSECONDARY: function (state, index) {
      state.secondaryList = state.filterList[index].sub_categories
      console.log(state.secondaryList)
    },
    SWITCHSHOPLIST: function (state, id) {
      state.reqId = id
      axios.get('http://localhost:3000/shoplist', {
        params: Object.assign({}, state.listReqObj, {
          'restaurant_category_ids[]': id
        })
      }).then(function (res) {
        console.log(res.data)
        state.listReqObj = Object.assign({}, state.listReqObj, {'restaurant_category_ids[]': id})
        state.msiteList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    SORTSHOPLIST: function (state, sortId) {
      axios.get('http://localhost:3000/shoplist', {
        params: Object.assign({}, state.listReqObj, {
          order_by: sortId
        })
      }).then(function (res) {
        console.log(res.data)
        state.listReqObj = Object.assign({}, state.listReqObj, {order_by: sortId})
        state.msiteList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    SETSUPPORT: function (state, idArr) {
      axios.get('http://localhost:3000/shoplist', {
        params: Object.assign({}, state.listReqObj, {
          support_ids: idArr
        })
      }).then(function (res) {
        console.log(res.data)
        state.listReqObj = Object.assign({}, state.listReqObj, {support_ids: idArr})
        state.msiteList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    SEARCH: function (state) {
      axios.get('http://localhost:3000/search', {
        params: state.location
      }).then(function (res) {
        state.searchList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    WEATHER: function (state) {
      axios.get('http://localhost:3000/weather', {
        params: state.location
      }).then(function (res) {
        console.log(res.data.image_hash)
        state.weatherList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    GETDISLIST: function (state, obj) {
      console.log(obj)
      axios.get('http://localhost:3000/district', {
        params: obj
      }).then(function (res) {
        console.log(res)
        state.disList = res.data
      }).catch(function (err) {
        console.log(err)
      })
    },
    SETLOCATION: function (state, obj) {
      console.log(obj)
      state.location = obj
      state.listReqObj = Object.assign({}, state.listReqObj, obj)
    },
    SETDISTRICT: function (state, str) {
      state.district = str
    },
    SHOPDETAILSMENU: function (state, id) {
      axios.get('http://localhost:3000/shopmenu', {
        params: {
          restaurant_id: id
        }
      }).then(function (res) {
        // console.log(res)
        var meunData = res.data
        console.log(meunData)
        state.shopDetailsMenu = meunData
      }).catch(function (err) {
        console.log(err)
      })
    },
    SEARCHTXT: function (state, txt) {
      axios.get('http://localhost:3000/searchList', {
        params: Object.assign({}, state.location, {
          keyword: txt
        })
      }).then(function (res) {
        // console.log(res.data['0'].restaurant_with_foods)
        state.searchWord = res.data['0'].restaurant_with_foods
      }).catch(function (err) {
        console.log(err)
      })
    },
    ULIKELIST: function (state) {
      axios.get('http://localhost:3000/ulike').then(function (res) {
        // console.log(res)
        var ulikeData = res.data.tags
        state.ulikeList = ulikeData
        // console.log(state.ulikeList)
      }).catch(function (err) {
        console.log(err)
      })
    },
    ULIKEVIEWARR: function (state) {
      axios.get('http://localhost:3000/ulikeview').then(function (res) {
        // console.log(res)
        var getViewData = res.data
        state.ulikeViewArr = getViewData
        console.log(state.ulikeViewArr)
      }).catch(function (err) {
        console.log(err)
      })
    },
    SEARCHTXT2: function (state) {
      var have = localStorage.getItem('searchWord')
      var arr = JSON.parse(have)
      axios.get('http://localhost:3000/searchList', {
        params: {
          keyword: arr[0]
        }
      }).then(function (res) {
        state.searchWord = res.data['0'].restaurant_with_foods
      }).catch(function (err) {
        console.log(err)
      })
    }
  },
  getters: {
    swiperList1: function (state) {
      return state.swiperList.slice(0, 8)
    },
    swiperList2: function (state) {
      return state.swiperList.slice(8)
    },
    cartss: function (state) {
      console.log(111)
      var arr = []
      for (var i = 0; i < state.carts; i++) {
        arr.push(state.carts.id)
      }
      return arr
    }
  }

})
